<?php
if ( ! defined( 'ABSPATH' ) )
	die( "Can't load this file directly" );

class DLProtectEditor
{
	function __construct() {
	}
	
	function action_admin_init() {
		// only hook up these filters if we're in the admin panel, and the current user has permission
		// to edit posts and pages
		if ( current_user_can( 'edit_posts' ) && current_user_can( 'edit_pages' ) ) {
			add_action('wp_ajax_dlprotect_editor', array($this, 'dlprotect_editor_callback'));
			add_filter( 'mce_buttons', array( $this, 'filter_mce_button' ) );
			add_filter( 'mce_external_plugins', array( $this, 'filter_mce_plugin' ) );
		}
	}

	function dlprotect_editor_callback() {
		global $wpdb; // this is how you get access to the database
	    $html = '<div id="dlprotect-form"><table id="dlprotect-table" class="form-table">\
				<tr>\
					<th><label for="dlprotect-file">File</label></th>\
					<td><input type="text" id="dlprotect-file" name="file" value="test.mp4" /><br />\
					<small>Choose the file to download</small></td>\
				</tr>\
				<tr>\
					<th><label for="dlprotect-visibility">Visibility</label></th>\
					<td><select name="visibility" id="dlprotect-visibility" value="">\
						<option value="private">Private</option>\
						<option value="public">Public</option>\
						</select><br />\
					<small>Choose whether to make this file private or not.</small></td>\
				</tr>\
				<tr>\
					<th><label for="dlprotect-linktext">Link Text</label></th>\
					<td><input type="text" name="linktext" id="dlprotect-linktext" value="" /><br />\
					<small>The text displayed to download this file.</small></td>\
				</tr>\
			</table>\
			<p class="submit">\
				<input type="button" id="dlprotect-submit" class="button-primary" value="Insert File" name="submit" />\
			</p>\
			</div>';
    echo $html;
	die();
}
	
	function filter_mce_button( $buttons ) {
		// add a separation before our button, here our button's id is "dlprotect_button"
		array_push( $buttons, '|', 'dlprotect_button' );
		return $buttons;
	}
	
	function filter_mce_plugin( $plugins ) {
		// this plugin file will work the magic of our button
		$plugins['dlprotect'] = WP_PLUGIN_URL . '/download-protect/resources/js/dlprotect_editor.js';
		return $plugins;
	}
	/**
	 * Creates an array of filenames from a particular directory, which match an array of accepted extentions.
	 * 
	 * @param String $directory The directory to search for files.
	 * @param Array $extensions A list of acceptable extensions to search for.
	 * 
	 * @return An array of filenames on success, false on failure.
	 */
	function getFileArray($directory, $extensions) {
		if (is_dir($directory)) { 
	  		$dir = opendir($directory);
			while(false != ($file = readdir($dir))) { //Search the directory for files
	    		if(($file != ".") and ($file != "..")) {
	      			$suffix = substr($file, strripos($file, '.')+1);
	      			if(in_array($suffix, $extensions)) { //interested in second chunk only
	      				$fileArray[] = $file;      
	      			}
	    		}
	 		}
	 		closedir($dir);
	 		if (isset($fileArray)) {
	 			sort($fileArray);
	 			return $fileArray;
	 		}
	 		else return false;
		}
		else return false;
	}
}